//
//  CTLoginViewController.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 riley chu. All rights reserved.
//

#import "CTLoginViewController.h"
#import "CTVerificationViewController.h"
#import "CTThirdPartyRegisterViewController.h"
#import "CTUserAgreementViewController.h"


#import "CTHeader.h"
//view
#import "CTLoginView.h"
#import "CTThirdPartyLoginView.h"

//logic
#import "CTLoginLogic.h"

//function
#import "CTDebugManager.h"
#import "CTRemindView.h"
#import "CTTextManager.h"
#import "CTAppLanguage.h"
#import "CTDeviceFunction.h"
#import "CTActivitorView.h"
#import "CTReachability.h"
#import "CTGenRequestData.h"
#import "CTUser.h"
#import "MobClick.h"

//model
#import "CTLoginItems.h"

//tool
#import "CRSA.h"

#import "CTPersonalGenderModifyView.h"

@interface CTLoginViewController ()<CTLogInViewDelegate,CTThirdPartyLogInViewDelegate,CTLoginLogicDelegate>
{
    CTLoginView * loginView;
    CTThirdPartyLoginView * tpLoginView;
    CTLoginLogic * loginLogic;
    NSString * userName;
    NSString * passWord;
    
    CTActivitorView * acView;
}

@end



@implementation CTLoginViewController


- (void)dealloc
{
    [loginView removeFromSuperview];
    loginView = nil;
    
    [tpLoginView removeFromSuperview];
    
    tpLoginView = nil;
    userName = nil;
    passWord = nil;
    
    [acView removeFromSuperview];
    acView = nil;
    
    
    [CTDebugManager showDebugLogInfo:@"dealloc : CTLoginViewController Is Removed!!!"];
}


- (instancetype)init
{
    self = [super init];
    if (self) {
        loginLogic = [[CTLoginLogic alloc] init];
        loginLogic.delegate = self;
        
    }
    return self;
}
-(void)viewDidAppear:(BOOL)animated
{
    //统计
    [MobClick beginLogPageView:@"CTLoginViewController"];
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated
{
    //统计
    [MobClick endLogPageView:@"CTLoginViewController"];
    [super viewWillDisappear:animated];
}


-(void)viewDidLoad
{
    [super viewDidLoad];
  
    
    loginView = [[CTLoginView alloc] init];
    loginView.delegate = self;
    [self.view addSubview:loginView];
    
    tpLoginView = [[CTThirdPartyLoginView alloc] init];
    tpLoginView.delegate = self;
    [self.view addSubview:tpLoginView];
}

#pragma mark - login view delegate

//取消返回
-(void)logInBackButton:(CTLoginView *)loginView
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

//获取输入手机号
- (void)logInUserNameTextFieldText:(NSString *)text withView:(CTLoginView *)logInView
{
    userName = text;
}
//获取输入密码
-(void)logInPassWordTextFieldText:(NSString *)text withView:(CTLoginView *)logInView
{
    passWord = text;
}
//登陆事件回调
- (void)logInButton:(CTLoginView *) loginView
{
    
    
    if ([[CTTextManager shareInstance] isBlankString:userName] == YES ) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_c" withValue:nil withTable:@"Language"]];
        return;
    }
    if ([[CTTextManager shareInstance] isBlankString:passWord] == YES) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_d" withValue:nil withTable:@"Language"]];
        return;
    }
    
    if ([loginLogic isPasswordEnable:passWord] == NO) {
        return;
    }
    
    //监测网络
    if ([self checkNetworkState] == NO)return;
    
    [self showRemindView];
    
    [loginLogic mobileLoginRequest:[CTGenRequestData genMobileLoginRequestItemUserName:userName withPassword:passWord] successCompletionHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeActivitorView];
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_e" withValue:nil withTable:@"Language"]];
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                
            }];
        });
    } failedHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            [acView removeActivitorView];
            CTRemindView * remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_b" withValue:nil withTable:@"Language"]];
        });
    }];
    
}

//注册事件回调
- (void)registerButton:(CTLoginView *) loginView
{
    CTVerificationViewController * verificationVC = [[CTVerificationViewController alloc] init];
    verificationVC.registerType = CTVerificationTypeMobileRegister;
    [self.navigationController pushViewController:verificationVC animated:YES];
}
//忘记密码事件回调
- (void)restPassWordButton:(CTLoginView *) loginView
{
    CTVerificationViewController * verificationVC = [[CTVerificationViewController alloc] init];
    verificationVC.registerType = CTVerificationTypeResetPassword;
    [self.navigationController pushViewController:verificationVC animated:YES];
}


#pragma mark - tp login view delegate-

-(void)qqLoginButton:(CTThirdPartyLoginView *)thirdPartyLoginView
{
    if ([self checkNetworkState] == NO)return;
    
    [self showRemindView];

    [loginLogic tpLoginRequestWithLoginType:CTTpLoginTypeQQ];
}

-(void)weChatLoginButton:(CTThirdPartyLoginView *)thirdPartyLoginView
{
    if ([self checkNetworkState] == NO)return;
    
    [self showRemindView];
    
    [loginLogic tpLoginRequestWithLoginType:CTTpLoginTypeWechat];
}

-(void)sinaLoginButton:(CTThirdPartyLoginView *)thirdPartyLoginView
{
    if ([self checkNetworkState] == NO)return;
    
    [self showRemindView];
    
    [loginLogic tpLoginRequestWithLoginType:CTTpLoginTypeWeiBo];
}

-(void)proButtonAction:(CTThirdPartyLoginView *)thirdPartyLoginView
{
    CTUserAgreementViewController * userAgreementVC = [[CTUserAgreementViewController alloc] init];
    userAgreementVC.showType = CTUserAgreementViewControllerShowTypePresent;
    [self presentViewController:userAgreementVC animated:YES completion:^{
        
    }];
}

#pragma mark - login logic delegate -

-(void)isPassWordEnable:(BOOL)flag description:(NSString *)descri
{
    if (flag == NO) {
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:descri];
    }
}

-(void)authorFailed:(CTLoginLogic *)logic
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [acView removeActivitorView];
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_j" withValue:nil withTable:@"Language"]];
    });
}

-(void)tpLoginSuccessed:(CTLoginLogic *)logic
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [acView removeActivitorView];
        CTUserType userType = [CTUser userType];
        if (userType == CTUserTypeNew) {
            //进入注册修改界面
            CTThirdPartyRegisterViewController * tpRegisterVC = [[CTThirdPartyRegisterViewController alloc] init];
            [self.navigationController pushViewController:tpRegisterVC animated:YES];
        }else{
            //直接登录
            [self.navigationController dismissViewControllerAnimated:YES completion:^{
                
            }];
        }
    });
}
-(void)tpLoginFailed:(CTLoginLogic *)logic withError:(NSError *)err
{
    dispatch_async(dispatch_get_main_queue(), ^{
        [acView removeActivitorView];
        CTRemindView * remindView = [[CTRemindView alloc] init];
        [remindView showRemindViewOn:self.view withRemindWord:[CTAppLanguage localizedStringForKey:@"remindView_login_b" withValue:nil withTable:@"Language"]];
    });
}



#pragma mark - check net work state -
-(BOOL)checkNetworkState
{
    NSString * descri;
    CTRemindView * remindView;
    
    CTNetWorkReachabilityStatus  status = [CTReachability judegeCurrentNetWorkStatus];
    switch (status) {
        case CTNetWorkReachabilityStatusUnknown:
            descri = [CTAppLanguage localizedStringForKey:@"remindView_login_networkUnknow" withValue:nil withTable:@"Language"];
            remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:descri];
            return NO;
            break;
        case CTNetWorkReachabilityStatusNotReachable:
            descri = [CTAppLanguage localizedStringForKey:@"remindView_login_networkReachable" withValue:nil withTable:@"Language"];
            remindView = [[CTRemindView alloc] init];
            [remindView showRemindViewOn:self.view withRemindWord:descri];
            return NO;
            break;
        default:
            break;
    }
    return YES;
}



#pragma mark - main thread -


-(void)showRemindView
{
    acView = [[CTActivitorView alloc] init];
    [acView showActivitorViewOn:self.view];
}


#pragma mark - memory warning


-(void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    [CTDebugManager showDebugLogInfo:@"CTLoginViewController内存警告!!!"];
}

@end
